var mongoose = require('mongoose');
var stock = require('tushare').stock;
var async = require('async');
var util = require('../util');
var list = require('../model').list;

var page = 1;

var count = {
    add: 0,
    del: 0,
    upd: 0
};

// 使用 getTodayAll接口获取所有的股票代码和名称
function record(page) {

    let pageOpt = {
        pageSize: 100,
        pageNo: page
    };

    stock.getTodayAll(pageOpt).then(({ data }) => {

        if (!data) {
            console.log('end................');
            return mongoose.disconnect();
        }

        async.eachLimit(data, 3, (stock, cb) => {

            let condition = {
                symbol: stock.symbol
            };
            let doc = {
                symbol: stock.symbol,
                code: stock.code,
                name: stock.name
            };

            util.mongo.insertDistinct(list, condition, doc, count, cb);

        }, err => {
            if (err) return console.log(err);
            console.log('add logs!');
            console.log(count);
            page++;
            record(page);
        });
    });
}

record(1);

exports.record = record;